//  准备引入 vuex
import { createStore } from "vuex";

const store = createStore({
  state: {
    loginToken: localStorage.getItem("ADMIN_LOGINTOKEN") || "", // 登录验证token
    loginUserId: JSON.parse(localStorage.getItem("ADMIN_LOGINUSERID")) || "", // 登录用户id
    loginUserName:
      JSON.parse(localStorage.getItem("ADMIN_LOGINUSERNAME")) || "", // 登录用户名
  },
  mutations: {
    // 更新token
    set_loginToken(state, value) {
      state.loginToken = value;
      if (value != "") {
        localStorage.setItem("ADMIN_LOGINTOKEN", JSON.parse(JSON.stringify(value)));
      } else {
        localStorage.removeItem("ADMIN_LOGINTOKEN");
      }
    },
    // 更新登录id
    set_loginUserId(state, value) {
      state.loginUserId = value;
      if (value != "") {
        localStorage.setItem("ADMIN_LOGINUSERID", JSON.stringify(value));
      } else {
        localStorage.removeItem("ADMIN_LOGINUSERID");
      }
    },
    // 更新登录用户名
    set_loginUserName(state, value) {
      state.loginUserName = value;
      if (value != "") {
        localStorage.setItem("ADMIN_LOGINUSERNAME", JSON.stringify(value));
      } else {
        localStorage.removeItem("ADMIN_LOGINUSERNAME");
      }
    },
  },
  getters: {},
  actions: {},
  modules: {},
});
export default store;
